/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;
import model.dao.DaoBase;
import model.dao.NotaJpaController;
import model.pojo.Aluno;
import model.pojo.Atividade;
import model.pojo.Nota;
import model.pojo.Professor;
import model.pojo.Turma;

/**
 *
 * @author leonidia
 */
public class CadastrarNotas extends javax.swing.JFrame {

    NotaJpaController notaJpaController = null;
    Professor professor;
    List<Atividade> listaA;
    List<Aluno> alunos;
    List<Turma> listaT;

    /**
     * Creates new form CadastrarNota
     * @param daoBase Cadastra as notas dos aluno do professor que faz a operacao e a envia para o notaJpaController inserir ela no BD.
     */
    public CadastrarNotas(DaoBase daoBase, Professor professor) {
        initComponents();
        notaJpaController = new NotaJpaController(null, daoBase.returnFactory());
        this.professor = professor;
        atualizaTurma();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        Turma = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        TabelaNotas = new javax.swing.JTable();
        Cadastrar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        Atividade = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Cadastrar Notas");

        Turma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TurmaActionPerformed(evt);
            }
        });

        TabelaNotas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome do Aluno", "Matricula", "Nota"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(TabelaNotas);

        Cadastrar.setText("Cadastrar");
        Cadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CadastrarActionPerformed(evt);
            }
        });

        jLabel2.setText("Turmas");

        Atividade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AtividadeActionPerformed(evt);
            }
        });

        jLabel3.setText("Atividade");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(181, 181, 181)
                                .addComponent(Cadastrar))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(Turma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(140, 140, 140)
                                .addComponent(jLabel1))
                            .addComponent(Atividade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Turma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(Atividade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38)
                .addComponent(Cadastrar))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CadastrarActionPerformed
        // TODO add your handling code here:
        for (int linha = 0; linha < TabelaNotas.getRowCount(); linha++) {
            Nota nota = new Nota(Float.parseFloat((String) TabelaNotas.getValueAt(linha, 2)), listaA.get(Atividade.getSelectedIndex()), alunos.get(linha));
            notaJpaController.create(nota);
        }
        dispose();
    }//GEN-LAST:event_CadastrarActionPerformed

    private void TurmaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TurmaActionPerformed
        // TODO add your handling code here:
        atualizaAtividade(listaT.get(Turma.getSelectedIndex()));
        atualizaTabela();
    }//GEN-LAST:event_TurmaActionPerformed

    private void AtividadeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AtividadeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_AtividadeActionPerformed
    /**
     * @param args the command line arguments
     */
    public void run() {
        setVisible(true);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox Atividade;
    private javax.swing.JButton Cadastrar;
    private javax.swing.JTable TabelaNotas;
    private javax.swing.JComboBox Turma;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    private void atualizaTabela() {
        DefaultTableModel model = (DefaultTableModel) TabelaNotas.getModel();
        alunos = (List<Aluno>) listaT.get(Turma.getSelectedIndex()).getDisciplina().getAluno();
        for (Aluno item : alunos) {
            model.addRow(new Object[]{item.getNome(), item.getMatricula(), null});
        }
    }

    public void atualizaTurma() {
        List<String> listaTurma = new ArrayList<String>();
        listaT = (List<Turma>) professor.getTurma();
        for (Turma item : listaT) {
            listaTurma.add("Ano: " + item.getAno() + " Local: " + item.getLocal() + " Disciplina: " + item.getDisciplina().getNome());
        }
        DefaultComboBoxModel defaultProfessor = new DefaultComboBoxModel(listaTurma.toArray());
        Turma.setModel(defaultProfessor);
    }

    public void atualizaAtividade(Turma turma) {
        List<String> listaAtividade = new ArrayList<String>();
        listaA = (List<Atividade>) turma.getAtividade();
        for (Atividade item : listaA) {
            listaAtividade.add("Nome: " + item.getNome() + " Tipo: " + item.getTipo() + " Valor: " + item.getValor());
        }
        DefaultComboBoxModel defaultAtividade = new DefaultComboBoxModel(listaAtividade.toArray());
        Atividade.setModel(defaultAtividade);
    }
}